
#include "jncFnWrapper.h"
#include <map>
#include <string>
#include <boost/any.hpp>
#include <iostream>


// bool testClass(jncTypev& x) {
//   return (x.type() == typeid(bool*))?true:false;
// }

static std::map<std::string, gReturnCode> Context;

gReturnCode getVarValue(std::string vn) {
  if(!Context.size() || Context.find(vn) == Context.end())
    return gReturnCode();
  return Context.find(vn)->second;
}

JncInterface* __use(JncInterface* o, jncTypev what) {
  std::string s;
  // try 
//     {
//       s = boost::any_cast<std::string>(what);
//     }
//   catch(boost::bad_any_cast&) {
//     std::cout << __FILE__ << __LINE__ << std::endl;
//   }
//   std::map<std::string, gReturnCode>::iterator i;
//   if((i = Context.find(s)) == Context.end())
//     assert(0);
  try {
    o->injectNewCopy(what);
  }
  catch(boost::bad_any_cast& e)
    {
      std::string msg("Failed conversion for ");
      msg += (std::string(s) + ":\n") + ((RnException*)&e)->what();
      throw RnException(((RnException*)&e)->getCode(), msg.c_str(), e);
    }
  return o;
}

void  __id(gReturnCode v, jncTypev name) {
  std::string s;
  try 
    {
      s = *(boost::any_cast<std::string*>(name));
    }
  catch(boost::bad_any_cast& e) {
    throw RnException(ISYMBOL, "Illegal Symbol", e);
  }
  Context[s] = v;
}
